export async function get(url: string) {
  const res = await fetch(process.env.NEXT_PUBLIC_API_BASE_URL + url);

  // 检查响应状态
  if (!res.ok) {
    throw new Error(`HTTP error! status: ${res.status}`);
  }

  // 检查内容类型
  const contentType = res.headers.get("content-type");
  if (!contentType || !contentType.includes("application/json")) {
    const text = await res.text();
    throw new Error(
      `Expected JSON response but got: ${text.substring(0, 100)}`
    );
  }

  return await res.json();
}

export async function post(url: string, body: any) {
  const res = await fetch(process.env.NEXT_PUBLIC_API_BASE_URL + url, {
    method: "POST",
    headers: {
      "Content-Type": "application/json",
    },
    body: JSON.stringify(body),
  });

  // 检查响应状态
  if (!res.ok) {
    throw new Error(`HTTP error! status: ${res.status}`);
  }

  // 检查内容类型
  const contentType = res.headers.get("content-type");
  if (!contentType || !contentType.includes("application/json")) {
    const text = await res.text();
    throw new Error(
      `Expected JSON response but got: ${text.substring(0, 100)}`
    );
  }

  return await res.json();
}
